﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WF_QLThuVien.Utilities
{
    public static class CKiemTra
    {
        public static bool IsNull(Control ct, string warning, ErrorProvider err)
        {
            if (ct.Text.Length <= 0)
            {
                err.SetError(ct, warning);
                ct.Focus();
                return true;
            }
            else
            {
                err.SetError(ct, "");
                return false;
            }
        }

        public static bool IsNumber(Control ct)
        {
            foreach (char c in ct.Text)
            {
                if (!Char.IsNumber(c))
                {
                    return false;                   
                }
            }
            return true;
        }

        public static bool LimitChar(string str, int min, int max)
        {
            int n = str.Length;
            if (n < min || n > max)
                return false;
            return true;
        }

        public static bool LimitChar(Control ct, int min, int max,ErrorProvider err,string warning)
        {
            int n = ct.Text.Length;
            if (n < min || n > max)
            {
                err.SetError(ct, warning);
                ct.Focus();
                return false;
            }
            else
            {
                err.SetError(ct, "");
                return true;
            }
        }

        public static bool IsNumber(object sender, KeyPressEventArgs e, Control ct, string warning, ErrorProvider err)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                err.SetError(ct, warning);
                return false;
            }
            else
            {
                err.SetError(ct, "");
                return true;
            }
        }

        public static bool IsChar(object sender, KeyPressEventArgs e, Control ct, string warning, ErrorProvider err)
        {
            if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !e.KeyChar.Equals(' '))
            {
                e.Handled = true;
                err.SetError(ct, warning);
                return false;
            }
            else
            {
                err.SetError(ct, "");
                return true;
            }
        }

        public static bool StartWith0(string str)
        {
            if (str[0] != 0)
            {
                return false;
            }
            return true;
        }

        public static bool StartWith0(Control ct, string warning, ErrorProvider err)
        {
            if (ct.Text[0] != 0)
            {
                err.SetError(ct, warning);
                ct.Focus();
                return false;
            }
            else
            {
                err.SetError(ct, warning);
                return true;
            }
        }
    }
}
